export default {
  data() {
    return {
      treeSearchName: '',
      selectedNodeList: [],
      ztreeObj: null,
    }
  },
  methods: {
    search_ztree(zTreeId) {
      this.ztreeObj = $.fn.zTree.getZTreeObj(zTreeId);
      if (this.treeSearchName) {
        this.searchNodes();
      }
    },
    searchNodes() {
      let _this = this;
      let treeObj = this.ztreeObj;
      let Val = this.treeSearchName;
      this.updateNodes(false);
      if (Val) {
        let txt = this.splitWord(Val);
        this.selectedNodeList = treeObj.getNodesByParamFuzzy('name', txt);
        this.updateNodes(true);
        if (this.selectedNodeList.length == 0) {
          this.showToast('没有搜索到相匹配的数据！', 2, 1000, function () {
            _this.treeSearchName = '';
          })
        } else {
          _this.showToast('搜索成功', 1);
        }
      }
    },
    updateNodes(highlight) {
      let zTree = this.ztreeObj,
        nodeList = this.selectedNodeList;
      for (var i = 0, l = nodeList.length; i < l; i++) {
        if (highlight && nodeList[i].getParentNode()) {
          zTree.expandNode(nodeList[i].getParentNode(), true, false, true);
        }
        nodeList[i].highlight = highlight;
        console.log("highlight", highlight)
        zTree.updateNode(nodeList[i]);
      }
    },
    splitWord(str) {
      if (str.indexOf("(") != -1) {
        var strs = str.split("(", 2);
        return strs[0];
      } else {
        return str;
      }
    },
  }
}
